package com.cszjzx.member.service;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cszjzx.member.dao.MemberDAO;
import com.cszjzx.member.dao.MemberDAOImpl;
import com.cszjzx.member.dao.MemberlockDAO;
import com.cszjzx.member.dao.MemberlockDAOImpl;
import com.cszjzx.member.dao.TempleMemberDAOImpl;
import com.cszjzx.member.po.Member;

public class MemberServiceImpl implements MemberService {

	private MemberDAO memberDao = null;
	
	public MemberServiceImpl() {
		try {
			memberDao = new MemberDAOImpl();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	@Override
	public Member getMemberByIdcard(String idcard) {
		Member member = null;
		try {
			/*1月12日，陈祥，在get(0)操作之前添加判断是否为null的操作，不然会发生数组越界的错误*/
			if(memberDao.find("idcard",idcard).size()!=0){
				member = memberDao.find("idcard",idcard).get(0);
			}else{
				member = null;
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			//学生不存在
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return member;
	}

	

	@Override
	public List<Member> getMembersByProcessAndMajor(int process, String major) {
		List<Member> list = null;
		List<Member> list2 = null;
		try {
			list = memberDao.find("major",major);
			list2 = new ArrayList<Member>();
			for (int i = 0 ; i < list.size() ; i ++) {
				if (Integer.parseInt(list.get(i).getValue("process")) == process) {
					list2.add(list.get(i));
				}
			}
			//从memberlock表中找出属于这个班级这个阶段的一组idcard
			MemberlockDAO memberlockDAO = new MemberlockDAOImpl();
			List<String> list3 = new ArrayList<String>(); 
			list3 = memberlockDAO.findMemberAndIslockByMajorAndProcess(major, Integer.toString(process));
			for(int i=0;i<list2.size();i++){
				Member m = list2.get(i);
				if(list3.contains(m.getValue("idcard"))){
					list2.get(i).setAudit(true);
				}
				else {
					list2.get(i).setAudit(false);
				}
			}
		} catch (SQLException e) {
			return null;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list2;
	}
	
	public void modifyPassword(Member member) {
		try {
			memberDao.update(member);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	
	public List<Member> getMembersByNameAndValue(String name,String value){
		try {
			return memberDao.find(name, value);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}


	@Override
	public boolean addMember(String studentId, String password,
			int departmentId, int majorId, String name, boolean sex,
			String idCard, Date birday, int age, String nationality,
			String parentLeague, String childLeague, String phoneNumber,
			String memberClass, String volunteer, String grade,
			String application, double cost, String memberCard, Date joinTime,
			Date graduateTime, Date outTime, String remark, int process,
			String state) {
		return false;
	}


	@Override
	public String inputMembersFromExcel(File file) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public List<Member> getMembersByMajorId(int majorId) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	public List<Member> findAll(){
		try {
			return memberDao.findAll();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}
